using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DataAccess;
using Entity; 
namespace Business
{
    public class bCustomer
    {
        public static int insertCustormer(eCustormer customer)
        {
            DBHelper dbh = new DBHelper();

            IList<SqlParameter> paramList = new List<SqlParameter>();
            SqlParameter pr = new SqlParameter("@CustomerUserName", SqlDbType.VarChar);
            pr.Value = customer.CustomerUserName;
            paramList.Add(pr);

            pr = new SqlParameter("@CustomerPassword", SqlDbType.VarChar);
            pr.Value = customer.CustomerPassword;
            paramList.Add(pr);

            pr = new SqlParameter("@CustomerFullName", SqlDbType.NVarChar);
            pr.Value = customer.CustomerFullName;
            paramList.Add(pr);

            pr = new SqlParameter("@CustomerAddress", SqlDbType.NVarChar);
            pr.Value = customer.CustomerAddress;
            paramList.Add(pr);

            pr = new SqlParameter("@CustomerPhone", SqlDbType.VarChar);
            pr.Value = customer.CustomerPhone;
            paramList.Add(pr);

            pr = new SqlParameter("@CustomerEmail", SqlDbType.NVarChar);
            pr.Value = customer.CustomerEmail;
            paramList.Add(pr);

            int r = dbh.ExecuteNonQuery("insertCustormer", CommandType.StoredProcedure, paramList);
            dbh.CloseConnection();
            return r;
        }

        public static int updateCustormer(eCustormer customer)
        {
            DBHelper dbh = new DBHelper();

            IList<SqlParameter> paramList = new List<SqlParameter>();

            SqlParameter pr = new SqlParameter("@CustomerID", SqlDbType.Int);
            pr.Value = customer.CustomerID;
            paramList.Add(pr);

            pr = new SqlParameter("@CustomerUserName", SqlDbType.VarChar);
            pr.Value = customer.CustomerUserName;
            paramList.Add(pr);

            pr = new SqlParameter("@CustomerPassword", SqlDbType.VarChar);
            pr.Value = customer.CustomerPassword;
            paramList.Add(pr);

            pr = new SqlParameter("@CustomerFullName", SqlDbType.NVarChar);
            pr.Value = customer.CustomerFullName;
            paramList.Add(pr);

            pr = new SqlParameter("@CustomerAddress", SqlDbType.NVarChar);
            pr.Value = customer.CustomerAddress;
            paramList.Add(pr);

            pr = new SqlParameter("@CustomerPhone", SqlDbType.VarChar);
            pr.Value = customer.CustomerPhone;
            paramList.Add(pr);

            pr = new SqlParameter("@CustomerEmail", SqlDbType.NVarChar);
            pr.Value = customer.CustomerEmail;
            paramList.Add(pr);

            int r = dbh.ExecuteNonQuery("insertCustormer", CommandType.StoredProcedure, paramList);

            dbh.CloseConnection();
            return r;
        }

        public static int deleteCustormer(int CustonerID)
        {
            DBHelper dbh = new DBHelper();

            IList<SqlParameter> paramList = new List<SqlParameter>();
            SqlParameter pr = new SqlParameter("@CustomerID", SqlDbType.Int);
            pr.Value = CustonerID;
            paramList.Add(pr);

            int r = dbh.ExecuteNonQuery("deleteCustormer", CommandType.StoredProcedure, paramList);
            dbh.CloseConnection();
            return r;
        }

        public static DataTable getAllCustomer()
        {
            DBHelper dbh = new DBHelper();
            DataTable dt = dbh.ExecuteQueryDataTable("getAllCustomer", CommandType.StoredProcedure, new List<SqlParameter>());
            dbh.CloseConnection();
            return dt;
        }
        // phuong thuc tra ve 1 doi tuong eCustomer
        //neu username pass sai tra ve null else not null
        public static eCustormer CustormerLogin(String UserName,String  Password)
        {
            IList<SqlParameter> paramList = new List<SqlParameter>();
            SqlParameter pr = new SqlParameter("@CustomerUserName", SqlDbType.VarChar);
            pr.Value = UserName;
            paramList.Add(pr);

            pr = new SqlParameter("@CustomerPassword", SqlDbType.VarChar);
            pr.Value = Password;
            paramList.Add(pr);

            IList il = bBase.LoadQueryString(typeof(eCustormer), "CustormerLogin", CommandType.StoredProcedure, paramList);
            if (il.Count > 0)
            {
                return (eCustormer)il[0];
            }
            else
            {
                return null;
            }
        }

        public static eCustormer getCustomerByID(int customerID)
        {
            IList<SqlParameter> paramList = new List<SqlParameter>();
            SqlParameter pr = new SqlParameter("@CustomerUserName", SqlDbType.Int);
            pr.Value = customerID;
            paramList.Add(pr);


            IList il = bBase.LoadQueryString(typeof(eCustormer), "getCustomerByID", CommandType.StoredProcedure, paramList);
            if (il.Count > 0)
            {
                return (eCustormer)il[0];
            }
            else
            {
                return null;
            }
        }
    }
}
